<?php

namespace app\admin\controller;

use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Setting extends Controller
{
    public function system()
    {
        $info = Db::name('sys_config')->where(['mark' => 'site'])->value('value');
        if (Request::isPost()) {
            $data = input();
            $post = json_encode($data);
            if (empty($info)) {
                $result = Db::name('sys_config')->insert(['mark' => 'site', 'classified' => 'site', 'value' => $post]);
            } else {
                $result = Db::name('sys_config')->where(['mark' => 'site'])->update(['value' => $post]);
            }

            if ($result > 0) {
                $ser = new \app\admin\service\SysConfig();
                $ser->resetConfig();
            }
            $this->success(lang('EDIT_SUCCESS'), $result);
        } else {
            $info = json_decode($info, true);
            View::assign('_info', $info);
            return View::fetch();
        }
    }

    public function service()
    {
        $info = Db::name('sys_config')->where(['mark' => 'company'])->value('value');
        if (Request::isPost()) {
            $data = input();
            $post = json_encode($data);
            if (empty($info)) {
                $result = Db::name('sys_config')->insert(['mark' => 'company', 'classified' => 'site', 'value' => $post]);
            } else {
                $result = Db::name('sys_config')->where(['mark' => 'company'])->update(['value' => $post]);
            }

            if ($result > 0) {
                $ser = new \app\admin\service\SysConfig();
                $ser->resetConfig();
            }
            $this->success(lang('EDIT_SUCCESS'), $result);
        } else {
            $info = json_decode($info, true);
            View::assign('_info', $info);
            return View::fetch();
        }
    }

    public function upload()
    {
        $info = Db::name('sys_config')->where(['mark' => 'upload'])->value('value');
        if (Request::isPost()) {
            $data = input();
            $post = json_encode($data);

            if (empty($info)) {
                $result = Db::name('sys_config')->insert(['mark' => 'upload', 'classified' => 'upload', 'value' => $post]);
            } else {
                $result = Db::name('sys_config')->where(['mark' => 'upload'])->update(['value' => $post]);
            }

            if ($result > 0) {
                $ser = new \app\admin\service\SysConfig();
                $ser->resetConfig();
            }
            $this->success(lang('EDIT_SUCCESS'), $result);
        } else {
            $info = json_decode($info, true);
            View::assign('_info', $info);
            return View::fetch();
        }
    }

    public function sms()
    {
        $info = Db::name('sys_config')->where(['mark' => 'sms'])->value('value');
        if (Request::isPost()) {
            $data = input();
            $post = json_encode($data);

            if (empty($info)) {
                $result = Db::name('sys_config')->insert(['mark' => 'sms', 'classified' => 'message', 'value' => $post]);
            } else {
                $result = Db::name('sys_config')->where(['mark' => 'sms'])->update(['value' => $post]);
            }

            if ($result > 0) {
                $ser = new \app\admin\service\SysConfig();
                $ser->resetConfig();
            }
            $this->success(lang('EDIT_SUCCESS'), $result);
        } else {
            $info = json_decode($info, true);
            View::assign('_info', $info);
            return View::fetch();
        }
    }
}